package com.lcxsoft.msmservice.service.impl;

import com.aliyuncs.CommonRequest;
import com.aliyuncs.CommonResponse;
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.IAcsClient;
import com.aliyuncs.http.MethodType;
import com.aliyuncs.profile.DefaultProfile;
import com.lcxsoft.commonutils.R;
import com.lcxsoft.msmservice.client.AbsRestClient;
import com.lcxsoft.msmservice.client.JsonReqClient;
import com.lcxsoft.msmservice.service.MsmService;
import com.lcxsoft.msmservice.utils.RandomUtil;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.util.Map;
import java.util.UUID;

@Service
public class MsmServiceImpl implements MsmService {

    static AbsRestClient InstantiationRestAPI() {
        return new JsonReqClient();
    }




    @Override
    public boolean send(Map<String, Object> param, String phone) {
        if (StringUtils.isEmpty(phone)){
            return false;
        }
        String code = (String) param.get("code");
        boolean isSend = sendSms(code, phone, "");
        return isSend;
    }

    @Override
    public boolean sendAlibaba(Map<String, Object> param, String phone) throws Exception{
        if (StringUtils.isEmpty(phone)){
            return false;
        }
        DefaultProfile profile = DefaultProfile.getProfile("default", "LTAI5tGzivBTSUw1ZbANXGKs", "otXnoZNJQSIvQ6tsKJnVUfbDz0YtFB");
        IAcsClient client = new DefaultAcsClient(profile);
        CommonRequest request = new CommonRequest();
        request.setMethod(MethodType.POST);
        request.setDomain("dysmsapi.aliyuncs.com");
        request.setVersion("2017-05-25");
        request.setAction("SendSms");
        request.putQueryParameter("PhoneNumbers", phone);
        request.putQueryParameter("SignName", "foodbox");
        request.putQueryParameter("TemplateCode", "SMS_232169454");
        request.putQueryParameter("TemplateParam", "{\"code\":\""+param.get("code")+"\"}");
        CommonResponse commonResponse = client.getCommonResponse(request);
        return commonResponse.getHttpResponse().isSuccess();
    }

    public static boolean sendSms(String param, String mobile, String uid){
        try {
            String result=InstantiationRestAPI().sendSms("a09094d35209417b16951b878ab6b33f", "337f9880f56c77d646db6332e48a0c27", "76d5706044824b3b95a81de7fb91390e", "610824", param, mobile, uid);
            System.out.println("Response content is: " + result);
            return true;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }
}
